package com.example.service.consumer.handler;

import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.client.ResponseErrorHandler;

import java.io.IOException;
import java.io.InputStream;

@Component
public class ResponseErrorHandlerImpl implements ResponseErrorHandler {

    /**
     *
     * @param response
     * @return 如果返回true，则代表此次rpc的服务器处理出现错误。否则代表没有错误
     */
    @Override
    public boolean hasError(ClientHttpResponse response) throws IOException {
        // 这里只是简单的判断是否出错。即响应200才认为正确。详细请参考HttpStatus
        if(response.getStatusCode() == HttpStatus.OK){
            return false;
        }
        return true;
    }

    /**
     * 当hasError返回true时，则会调用handleError处理错误
     * @param response 此次rpc的响应，其实现类是SimpleClientHttpResponse
     */
    @Override
    public void handleError(ClientHttpResponse response) throws IOException {
        System.out.println("响应状态码:  "+response.getStatusCode());
        System.out.println("响应的头信息:  "+response.getHeaders());
    }

}
